package com.wan37.logic.guild.dao.impl;

import com.wan37.logic.guild.dao.GuildWarehouseDao;
import com.wan37.logic.guild.domain.entity.GuildWarehouseDO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-03 17:51
 **/
@Repository
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class GuildWarehouseDaoImpl implements GuildWarehouseDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Override
    public List<GuildWarehouseDO> getGuildWarehouseDOByGuildId(String guildId) {
        return (List<GuildWarehouseDO>) hibernateTemplate.find("from " +
                "GuildWarehouseDO where guildId=?", guildId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByGuildIdAndBackpackId(String guildId, String backpackId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from GuildWarehouseDO where guildId=? and backpackId=?");
        query.setParameter(0, guildId);
        query.setParameter(1, backpackId);
        query.executeUpdate();
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByGuildId(String guildId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from GuildWarehouseDO where guildId=?");
        query.setParameter(0, guildId);
        query.executeUpdate();
        return false;
    }

    @Override
    public boolean add(GuildWarehouseDO guildWarehouseDO) {
        hibernateTemplate.save(guildWarehouseDO);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        return false;
    }

    @Override
    public boolean update(GuildWarehouseDO guildWarehouseDO) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
//        Query query = session.createQuery("update GuildWarehouseDO set ");
        return false;
    }

    @Override
    public List<GuildWarehouseDO> getAll() {
        return null;
    }
}
